var CourseCreationController = (function () {

	var courseOfferingCode;
	var lessonNo = 1;

	function initTree() {
		$("#jstree").on("select_node.jstree", function (e, data) {
			for (var i = 0; i < data.selected.length; i++) {
				selectedNode = $("#" + data.instance.get_node(data.selected[i]).id);
			}
		}).jstree();
	}


	function initialize() {

		initTree();

		$(".action").bind("click", function () {
			var btnId = $(this).attr("id");
			$('#modal').modal({
				show: true
			})
			if (selectedNode == null) {
				alert("Please select a node in the structure tree");
			} else {
				if (selectedNode.attr("type") == "CourseOffering") {

					if (btnId == "add") {
						$("#holder-parent").show();
						$("#form-holder").empty();
						$('#form-holder').load("../course/lesson_creation.html", function () {
							alert("Loaded lesson");
							$('#btnCreateLesson').on('click', function () {
								alert("Adding Lesson");
								CourseCreationController.addLesson();
							});
						});
					} else if (btnId == "edit") {}
				} else if (selectedNode.attr("type") == "Lesson") {
					if (btnId == "add") {
						$('#modalBody').empty();
						$("#title").html("Add Lesson Unit");
						$('#modalBody').load("../course/lesson_creation.html");
						$('#btnCreateLesson').off('click');
						$('#btnCreateLesson').on('click', function () {
							CourseCreationController.addUnit();
						});
					} else if (btnId == "edit") {}
				} else if (selectedNode.attr("type") == "Unit") {
					if (btnId == "add") {
						$('#modalBody').empty();
						$("#title").html("Add Lesson Unit");
						$('#modalBody').load("../course/lesson_creation.html");
						$('#btnCreateLesson').off('click');
						$('#btnCreateLesson').on('click', function () {
							CourseCreationController.addUnit();
						});
					} else if (btnId == "edit") {}
				}
			}
		});

		Handlebars.registerPartial("units-recursion", $("#units-recursion-partial").html());
	}

	function addCourseOffering() {
		var owners = new Array("sahsharm");
		var courseOffering = {
			code: $("#co_code").val(),
			title: $("#co_title").val(),
			description: tinyMCE.activeEditor.getContent(),
			owners: owners,
			state: $("#co_state").val(),
			visibility: $("#co_vis").val(),
			price: $("co_price").val(),
			mode: $("#co_mode").val()
		}
		CourseMgtService.addCourseOffering(courseOffering, addCourseOffering_Result);
	}

	function addCourseOffering_Result(data) {
		alert(JSON.stringify(data));
	}


	function addLesson() {
		var owners = new Array("sahsharm");
		var lesson = {
			name: $("#lesson_name").val(),
			title: $("#lesson_title").val(),
			description: tinyMCE.activeEditor.getContent(),
			owners: owners,
			state: $("#lesson_state").val(),
		}
		CourseMgtService.addLesson(lesson, "12", addLesson_Result);
	}

	function addLesson_Result(data) {
		alert(JSON.stringify(data));
		var source = $("#courseoffering-details-nav-tmpl").html();
		var templ = Handlebars.compile(source);
		$("#treecontainer").empty();
		$("#treecontainer").append("<div id='jstree'></div>");
		$("#jstree").append(templ(data));
		initTree();
	}

	function addUnit() {
		var owners = new Array("sahsharm");
		var unit = {
			name: $("#lesson_name").val(),
			title: $("#lesson_title").val(),
			description: tinyMCE.activeEditor.getContent(),
			owners: owners,
			state: $("#lesson_state").val(),
		}
		CourseMgtService.addLessonUnit(unit, "12", addUnit_Result);
	}

	function addUnit_Result(data) {
		alert(JSON.stringify(data));
		var source = $("#courseoffering-details-nav-tmpl").html();
		var templ = Handlebars.compile(source);
		$("#treecontainer").empty();
		$("#treecontainer").append("<div id='jstree'></div>");
		$("#jstree").append(templ(data));
		initTree();
	}


	return {
		initialize: initialize,
		addCourseOffering: addCourseOffering,
		addCourseOffering_Result: addCourseOffering_Result,
		addLesson: addLesson,
		addLesson_Result: addLesson_Result,
		addUnit: addUnit,
		addUnit_Result: addUnit_Result
	};

})();